<?php
require_once 'inc.php';
?>

<style>
.color_div {
	float:left;
	width:50px;
	height:50px;
	line-height:25px;
	text-align:center;
}
</style>

<?php
$p_limit = 0.1;
$get_c = $_GET['c'];
if ($get_c)
{
	$result = sys::sql("select * from test_image_pallette where color_hex='%s' and percentage>%f order by percentage desc limit 20;",
			$get_c, $p_limit);
	//print_r($result);
	foreach ($result as $v)
	{
		echo sprintf("<a target=_blank href=analysis.php?f=%s><img border=0 src=r.php?f=%s />",
			$v['image_path'], rawurlencode($v['image_path']));
		//echo sprintf("<a target=_blank href=%s><img border=0 src=r.php?f=%s />",
		//	$v['image_path'], rawurlencode($v['image_path']));
	}
	exit;
}

$result = sys::sql("select color_hex, count(color_hex) as count from test_image_pallette "
			." where percentage>%f group by color_hex order by color_hex desc;", $p_limit);

foreach ($result as $v)
{
	//echo sprintf("<a target=_blank href=?c=%s><div class=color_div style='background:#%s;'>%s<br>%d</div></a>",
	//		$v['color_hex'], $v['color_hex'], $v['color_hex'], $v['count']);
	echo sprintf("<a target=_blank href=?c=%s><div class=color_div style='background:#%s;'>%d</div></a>",
			$v['color_hex'], $v['color_hex'], $v['count']);
}

/*$data = file('colors.txt');
$data = array_slice($data, 0, 1000);

foreach ($data as $line)
{
	$t = explode("\t", $line);
	$file = array_shift($t);

	$colors = array();
	for ($i=0; $i<50; $i++)
	{
		//print_r($t);exit;
		$v = trim(array_shift($t));
		if (!strlen($v)) continue;
		
		$tt = explode("|", $v);
		$color = trim($tt[0]);
		$count = trim($tt[1]);
		$colors[$color] = $count;
	}
	
	$total_count = array_sum($colors);
	$total_width = 800;
	echo "<div>";
	foreach ($colors as $color => $count)
	{
		echo sprintf("<div style='float:left;width:%fpx;height:20px;background:#%s;'></div>", $count*$total_width/$total_count, $color);
	}
	
	echo sprintf("<a target=_blank href='%s'>%s</a>", $file, $file);
	echo "</div>\n";
}*/
/*
ksort($colors);

echo '<pre>';
foreach ($colors as $color => $files)
{
	$links = array();
	foreach ($files as $key => $file)
	{
		$links[] = sprintf("<a href='%s'>%d</a>", $file, $key);
	}
	echo sprintf("<div style='float:left;width:50px;height:50px;background:#%s;'>%s</div>", $color, implode(" ", $links));
}*/